package com.example.myapplication.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.myapplication.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.item_click_layout)
public class ItemClickActivity extends AppCompatActivity {

    @ViewById
    ListView listView;
    private LayoutInflater inflater;
    private String[] mTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater = LayoutInflater.from(this);
    }

    @AfterViews
    public void iniViews() {
        mTitles = new String[]{
          "item1","item2","item3","item4","item5","item6","item7","item8"
        };
        listView.setAdapter(new MyAdapter());
    }
    // 单项点击事件注入
    @ItemClick(R.id.listView)
    public void showItemClick(String str) {
        Log.d("@ItemClick", str);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mTitles.length;
        }

        @Override
        public Object getItem(int position) {
            return mTitles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                // 加载布局
                convertView = inflater.inflate(R.layout.item_listview,parent,false);
                holder = new ViewHolder();
                holder.textView = (TextView) convertView.findViewById(R.id.textView);
                convertView.setTag(holder);
            } else {
                // convertView已被复用
                holder = (ViewHolder) convertView.getTag();
            }

            holder.textView.setText(mTitles[position]);
            return convertView;
        }

        private class ViewHolder {
            TextView textView;
        }
    }
}
